#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#define  BUFSIZE            (100)
#define  ADRESS             "127.0.0.3"
#define  UDPPORT               (19734)
#define  OUT
#define  ARGUM               (3)
#define UDPMAXMESSAGE        65535



static int SocketNew(int* _socket,struct sockaddr_in* _saddr,const char* _name)
{
    *_socket = socket( AF_INET /*1st argument is the purpose AF_UNIX local*/ ,
               SOCK_DGRAM  /*communication UDP connection*/,
                0/*Protocol*/);

    if(*_socket ==-1)
    {
        perror("socket");
        return 1;
    }
    _saddr->sin_family = AF_INET;/*dueto AF_UNIX unix(7) instructions*/
    _saddr->sin_addr.s_addr = inet_addr(ADRESS);/*dueto AF_UNIX unix(7) instructions)*/
    _saddr->sin_port = htons( UDPPORT);/*convert byte representation on number to network represent*/
    return 0;
}

/*Add to the massage Header at sizeof int + 1 and send to server*/
static int AddHeader(char* buf1,char* buf2)
{
    *(int*)buf2 = strlen(buf1)+1+sizeof(int);
    strcat((buf2 + sizeof(int)),buf1);
    return  *(int*)buf2;
}

int main()
{
    int                 client_sockfd;
    char                recievbuf[100];
    struct sockaddr_in  client_saddr;
    int                 wr;

    char*               mes3="UDP Client  Waiting for message   : ";
    int                 ln;
    char                headbuffer[BUFSIZE];


    if(SocketNew(&client_sockfd,&client_saddr,0) )
    {
        fprintf(stderr,"\nUDP CLinet Socket New problems!!!");
        exit(EXIT_FAILURE);
    }

   /* err = connect(client_sockfd,(struct sockaddr*)&client_saddr,sizeof(client_saddr));
    if(err == -1)
    {
        perror("connect");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }*/
/*    close(err);*/
    printf("\n Client 3 try to connect... \n");
    while(1)
    {
        memset(headbuffer,0,sizeof(headbuffer));
        ln=AddHeader(mes3,OUT headbuffer);

        if (ln < UDPMAXMESSAGE)
        {
            wr= sendto(client_sockfd,headbuffer,ln,0,(struct sockaddr*)&client_saddr,sizeof(client_saddr));
            if(wr == -1 && errno == EPIPE)
            {
                perror(" UDP mes send :    ");
                break;
            }
        }
        else
        {
            printf("\nMessage size oversize the maximum allowed");
        }
        ln = sizeof(client_saddr);

        wr = recvfrom(client_sockfd,recievbuf,BUFSIZE,0,
                            (struct sockaddr*)&client_saddr,(socklen_t*) &ln);

        printf("\n Recieve message  : \" %s \" \n",recievbuf);

        if(recievbuf[0])
        {
            sleep(2);
        }
        recievbuf[0]=0;
        if(wr == -1 && errno == EPIPE)
        {
            break;
        }
    }
  close(client_sockfd);
  printf("\nClient 3 finished\n");
    return 0;
}
